package com.chianghao.rock.system.service.organization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chianghao.core.exception.HaoException;
import com.chianghao.core.frame.service.BaseService;
import com.chianghao.rock.system.dao.organization.OrganizationDao;
import com.chianghao.rock.system.dao.organization.OrganizationMapper;
import com.chianghao.rock.system.entity.Organization;

@Service
public class OrganizationService extends BaseService<OrganizationMapper, Organization> {

	
	@Autowired
	OrganizationDao dao;
	
	/**
	 * 遍历删除子节点
	 * @param id
	 */
	public void doDelete(Long id) {
		List<Organization> children = queryChildren(id);
		if(children!=null&&!children.isEmpty()) {
			for(Organization item:children) {
				doDelete(item.getId());
			}
		}
		this.deleteById(id);
	}

	
	public List<Organization> queryChildren(Long parentId){
		Organization where  = new Organization();
		where.setParentId(parentId);
		return this.mapper.queryListByEntity(where, null);
	}

	
	public void getAllNodeChildren(Long parentId,List<Organization> list) {
		List<Organization> children  = queryChildren(parentId);
		if(children!=null&&children.size()>0) {
			for(Organization a:children) {
				list.add(a);
				getAllNodeChildren(a.getId(),list);
			}
		}
	}
	
	private boolean checkParentIsChildNode(Long id,Long parentId) {
		List<Organization>  list  = new ArrayList<Organization>();
		getAllNodeChildren(id,list);
		for(Organization o:list) {
			if(o.getId().longValue()== parentId.longValue()) {
				return true;
			}
		}
		return false;
	}
	
	public void doModifyOrganization(Organization organization) {
		//校验父节点是否指向当前节点的子节点
		if(organization.getParentId()!=null&&checkParentIsChildNode(organization.getId(),organization.getParentId())) {
			throw new HaoException("error:parent.id.can.not.child.node");
		}
		
		this.updateById(organization, organization.getId());
	}


	public Map<Long, Organization> queryOrganizationMap(Set<Long> orgList) {
		Map<Long,Organization> map = new HashMap<Long,Organization>();
		if(orgList==null) {
			return map;
		}
		orgList.remove(null);
		if(orgList.size()==0) {
			return map;
		}
		List<Organization> list  = this.dao.queryByIds(new ArrayList<Long>(orgList));
		
		for(Organization o:list) {
			map.put(o.getId(), o);
		}
		return map;
	}


	public Set<Long> getAllChildId(Long organizationId) {
		List<Organization> list =  queryChildren(organizationId);
		if(list!=null&&list.size()>0) {
			Set<Long> ids = new HashSet<Long>();
			list.forEach(item->{
				ids.add(item.getId());
				Set<Long> childIds  = getAllChildId(item.getId());
				if(childIds!=null&&!childIds.isEmpty()) {
					ids.addAll(childIds);
				}
			});
			return ids;
		}
		return null;
	}
	

}